//--------------------------------------------------------------------------------
// This file is part of the Windows Workflow Foundation Sample Code
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// This source code is intended only as a supplement to Microsoft
// Development Tools and/or on-line documentation.  See these other
// materials for detailed information regarding Microsoft code samples.
// 
// THIS CODE AND INFORMATION ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
//--------------------------------------------------------------------------------

using System;
using System.Drawing.Design;
using System.ComponentModel;
using System.Windows.Forms;
using System.Windows.Forms.Design;
using System.Workflow.ComponentModel;

namespace Microsoft.Samples.Workflow.UI.Design
{
    class TransitionTargetEditor : UITypeEditor
    {
        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            Transition transition = context.Instance as Transition;
            if (transition == null)
            {
                throw new ArgumentException(SR.GetString(SR.Error_UnexpectedArgumentType, typeof(Transition).FullName, "context.Instance"));
            }

            Navigator navigator = (transition.Activity != null) ? transition.Activity.Parent as Navigator : null;
            IWindowsFormsEditorService editorService = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
            if (navigator != null && editorService != null)
            {
                ListBox dropDownList = new ListBox();
                dropDownList.BorderStyle = BorderStyle.None;

                foreach (Activity child in navigator.EnabledActivities)
                {
                    if (child.QualifiedName != transition.Activity.QualifiedName)
                    {
                        dropDownList.Items.Add(child.QualifiedName);
                    }
                }

                object selectedObject = null;
                dropDownList.SelectedIndexChanged += delegate(object sender, EventArgs e)
                {
                    editorService.CloseDropDown();
                    if (dropDownList.SelectedIndex < 0)
                    {
                        return;
                    }

                    if (dropDownList.Items.Count > dropDownList.SelectedIndex)
                    {
                        selectedObject = dropDownList.Items[dropDownList.SelectedIndex];
                    }
                };

                editorService.DropDownControl(dropDownList);
                if (dropDownList.SelectedIndex != -1 && selectedObject != null)
                {
                    return selectedObject;
                }
            }
            return value;
        }
        public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext typeDescriptorContext)
        {
            return UITypeEditorEditStyle.DropDown;
        }
    }
}
